import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../data_picker/bottom.dart';

///***********************************************
///文件描述：
///创建人：dgg108698
///创建时间：2020/11/24
///修改人：
///修改时间：
///修改备注：
///***********************************************

Future<int> cpsShowBottomSingleWheel({
  @required BuildContext context,
  @required int itemCount,
  @required String Function(int index) strGetter,
  String title,
  int initialIndex,
}) {
  return cpsShowBottomDialog<int>(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10)),
      ),
      builder: (context) {
        return _SingleWheel(
          itemCount: itemCount,
          strGetter: strGetter,
          title: title,
          initialIndex: initialIndex,
        );
      });
}

class _SingleWheel extends StatefulWidget {
  final String title;
  final int itemCount;
  final int initialIndex;
  final String Function(int index) strGetter;

  _SingleWheel({
    @required this.itemCount,
    @required this.strGetter,
    this.initialIndex,
    this.title,
  });

  @override
  State<StatefulWidget> createState() => _SingleWheelState();
}

class _SingleWheelState extends State<_SingleWheel> {
  FixedExtentScrollController _controller;
  int _selectedItem;

  @override
  void initState() {
    super.initState();
    final initial = widget.initialIndex ?? 0;
    _selectedItem = initial;
    _controller = FixedExtentScrollController(initialItem: initial);
  }

  @override
  Widget build(BuildContext context) {
    return CpsBottomDialog.form(
      title: Text(widget.title ?? '选择'),
      negative: Text(''),
      contentPadding: EdgeInsets.all(0),
      positive: Icon(Icons.cancel_outlined, size: 25,color: Color(0xFF999999)),
      onTapPositive: () {
        Navigator.pop(context, _selectedItem);
      },
      onTapNegative: () {
        Navigator.pop(context);
      },
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            height: 230,
            child: CupertinoPicker.builder(
              scrollController: _controller,
              itemExtent: 50.0,
              onSelectedItemChanged: (index) {
                _selectedItem = index;
              },
              itemBuilder: _buildItem,
              childCount: widget.itemCount,
              diameterRatio: 1.0,
            ),
          ),
          confirmBtn(
              title: "确定",
              padding: EdgeInsets.all(12.5),
              call: () {
                Navigator.pop(context, _selectedItem);
              })
        ],
      ),
    );
  }

  ///确认按钮
  static Widget confirmBtn(
      {String title,
      Function call,
      EdgeInsetsGeometry padding,
      double height,
      BorderRadius borderRadius,
      FontWeight fontWeight,
      Color btnColor,
      Color textColor,
      bool canClick = true}) {
    return Padding(
      padding: padding ??
          EdgeInsets.only(
              left: 10, right: 10, bottom: 15),
      child: FlatButton(
        shape: RoundedRectangleBorder(
            borderRadius:
                borderRadius ?? BorderRadius.all(Radius.circular(10))),
        color: btnColor ?? Color(0xFFFFD433),
        onPressed: canClick
            ? () {
                call.call();
              }
            : null,
        disabledColor: Color(0xFFF5F5F5),
        padding: EdgeInsets.zero,
        child: Container(
          height: height ?? 44,
          alignment: Alignment.center,
          width: double.infinity,
          child: Text(
            title ?? "提交",
            style: TextStyle(
                color: canClick ? textColor ?? Color(0xFF202020) : Colors.white,
                fontSize: 18,
                fontWeight: fontWeight ?? FontWeight.w500),
          ),
        ),
      ),
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.symmetric(horizontal: 16.0),
      child: Text(
        widget.strGetter(index),
        maxLines: 1,
        style: Theme.of(context).textTheme.subtitle1,
      ),
    );
  }
}
